package com.lsfb.signln.Statistics.LeaveType;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.lsfb.signln.Leave.PersonnelSelect.PersonnelSelectActivity;
import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.LsfbTimePicker;
import com.lsfb.signln.Utils.MyProgressDialog;
import com.lsfb.signln.Utils.PopWindowManager;
import com.lsfb.signln.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@ContentView(R.layout.aty_leave_type_statistics)
public class LeaveTypeStatisticsActivity extends LsfbActivity {

    @ViewInject(R.id.fg_leave_top)
    private LinearLayout fg_leave_top;

    /**
     * 指定人员
     */
    @ViewInject(R.id.fg_text_leave_creator)
    private TextView text_leave_creator;

    /**
     * 指定时间
     */
    @ViewInject(R.id.fg_text_leave_state)
    private TextView text_leave_state;

    /**
     * 暂无数据
     */
    @ViewInject(R.id.aty_statistics_leave_layout_null)
    private RelativeLayout layout_null;

    /**
     * 饼状图统计
     */
    @ViewInject(R.id.chart)
    private PieChart chart;

    /**
     * PopWindow管理类
     */
    private PopWindowManager replay;

    //timePicker
    private LsfbTimePicker starttime, endtime;

    //人员类型
    private String usertype = "2";
    //下属人员
    private String users = null;
    //部门
    private String depars = null;
    //时间类型
    private int timetype = 0;
    //开始时间
    private String start_time = "";
    //结束时间
    private String end_time = "";

    private MyProgressDialog dialog;

    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "免定位分类统计");
        dialog = new MyProgressDialog(this, "加载中......");
        getLeaveTypeStatistics();

        starttime = new LsfbTimePicker(this);
        starttime.SelectSureListenner(new LsfbTimePicker.ClickSureListener() {
            @Override
            public void ClickSure(String time) {
                start_time = time;
                showPop(start_time, end_time, timetype);
            }
        });

        endtime = new LsfbTimePicker(this);
        endtime.SelectSureListenner(new LsfbTimePicker.ClickSureListener() {
            @Override
            public void ClickSure(String time) {
                end_time = time;
                showPop(start_time, end_time, timetype);
            }
        });
    }

    private void setData(LeaveTypeStatisticsBean bean) {
        List<PieEntry> entries = new ArrayList<>();
        entries.add(new PieEntry(bean.getType1percent(), "因事"));
        entries.add(new PieEntry(bean.getType2percent(), "因病"));
        entries.add(new PieEntry(bean.getType3percent(), "年休"));
        entries.add(new PieEntry(bean.getType4percent(), "其他"));

        List<Integer> color = new ArrayList<>();
        color.add(getResources().getColor(R.color.colorAccent));
        color.add(getResources().getColor(R.color.orange_color));
        color.add(getResources().getColor(R.color.green_color));
        color.add(getResources().getColor(R.color.red_color));

        PieDataSet set = new PieDataSet(entries, "");
        set.setColors(color);
        //数值描述放置的位置及延长线长度
        set.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        set.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        set.setValueLinePart1Length(0.6f);
        set.setValueLinePart2Length(0.6f);

        set.setValueLineColor(getResources().getColor(R.color.theme_font));

        PieData data = new PieData(set);
        //设置百分比字体颜色
        data.setValueTextColor(getResources().getColor(R.color.theme_font));
        //百分号
        data.setValueFormatter(new PercentFormatter());

        chart.setData(data);

        //不显示详细文字,省份等
        chart.setDrawEntryLabels(false);
        //使用百分比
        chart.setUsePercentValues(true);
        //设置pieChart图表上下左右的偏移，类似于外边距
        chart.setExtraOffsets(20, 20, 20, 10);

        chart.highlightValues(null);

        //不可转动
        chart.setRotationEnabled(false);

        //设置控件背景颜色
        chart.setBackgroundColor(getResources().getColor(R.color.bg_color));
        chart.animateY(1400);

//        设置显示在图表右下角的描述文本。
        Description description = new Description();
        description.setText("免定位分类统计图");
        description.setEnabled(false);
        chart.setDescription(description);

        //设置中间圆孔是否显示
        chart.setDrawHoleEnabled(false);
        //设置分类名称字体颜色
        chart.setEntryLabelColor(getResources().getColor(R.color.theme_font));
        chart.setEntryLabelTextSize(10);

        //图例
        Legend legend = chart.getLegend();
        legend.setOrientation(Legend.LegendOrientation.VERTICAL);
        legend.setForm(Legend.LegendForm.SQUARE); //设置图例的形状
        //图例的位置
        legend.setPosition(Legend.LegendPosition.RIGHT_OF_CHART_CENTER);
        legend.setWordWrapEnabled(true);

        legend.setXEntrySpace(2f);//设置距离饼图的距离，防止与饼图重合
        legend.setYEntrySpace(2f);

        //设置背景色
        chart.setBackgroundColor(getResources().getColor(R.color.white_color));

        //百分比形式绘制
        chart.setUsePercentValues(true);

        chart.invalidate(); // refresh
    }

    /**
     * 获取请假分类统计
     */
    private void getLeaveTypeStatistics() {
        HashMap hashMap = new HashMap();
        hashMap.put("type", usertype);
        if (usertype.equals("1")) {
            if (depars != null) {
                hashMap.put("deps", depars);
            }
            if (users != null) {
                hashMap.put("persons", users);
            }
        }
        hashMap.put("timetype", String.valueOf(timetype));
        if (timetype == 3) {
            hashMap.put("starTime", start_time);
            hashMap.put("endTime", end_time);
        }
        dialog.showdialog();
        new BaseInternet().getData(URLString.LEAVEREASON, hashMap, LeaveTypeStatisticsBean.class,
                new LeaveTypeStatisticsEvent<LeaveTypeStatisticsBean>(), true);
    }

    @EventAnnotation
    public void eventResult(LeaveTypeStatisticsEvent<LeaveTypeStatisticsBean> event) {
        dialog.hidedialog();
        switch (event.getCode()) {
            case 200:
                if (event.getData().getType1count() == 0 && event.getData().getType2count() == 0 && event.getData().getType3count() == 0 && event.getData().getType4count() == 0) {
                    layout_null.setVisibility(View.VISIBLE);
                    chart.setVisibility(View.GONE);
                } else {
                    layout_null.setVisibility(View.GONE);
                    chart.setVisibility(View.VISIBLE);
                    setData(event.getData());
                }
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    /**
     * 显示popWindows
     */
    private void showPop(final String start_time, final String end_time, final int time_type) {
        replay = new PopWindowManager(this, R.layout.pop_without_address_date) {
            @Override
            public void onViewLister(View contentView, final PopWindowManager manager) {
                //本月
                final RelativeLayout rl_curmonth = (RelativeLayout) contentView.findViewById(R.id.pop_without_address_month);
                final ImageView iv_curmonth = (ImageView) contentView.findViewById(R.id.pop_without_address_month_img);
                //本季
                final RelativeLayout rl_curquarter = (RelativeLayout) contentView.findViewById(R.id.pop_select_date_bottom);
                final ImageView iv_curquarter = (ImageView) contentView.findViewById(R.id.pop_select_date_bottom_img);
                //本年
                final RelativeLayout rl_curyear = (RelativeLayout) contentView.findViewById(R.id.pop_select_personnel);
                final ImageView iv_curyear = (ImageView) contentView.findViewById(R.id.pop_select_personnel_img);
                //确定
                final TextView btn_sure = (TextView) contentView.findViewById(R.id.pop_select_change);
                //开始时间
                final TextView tv_starttime = (TextView) contentView.findViewById(R.id.pop_select_starttime_tv);
                //结束时间
                final TextView tv_endtime = (TextView) contentView.findViewById(R.id.pop_select_endtime_tv);

                switch (time_type) {
                    //本月
                    case 0:
                        iv_curmonth.setVisibility(View.VISIBLE);
                        iv_curquarter.setVisibility(View.GONE);
                        iv_curyear.setVisibility(View.GONE);
                        break;
                    //本季
                    case 1:
                        iv_curmonth.setVisibility(View.GONE);
                        iv_curquarter.setVisibility(View.VISIBLE);
                        iv_curyear.setVisibility(View.GONE);
                        break;
                    //本年
                    case 2:
                        iv_curmonth.setVisibility(View.GONE);
                        iv_curquarter.setVisibility(View.GONE);
                        iv_curyear.setVisibility(View.VISIBLE);
                        break;
                    //自定义事件
                    case 3:
                        iv_curmonth.setVisibility(View.GONE);
                        iv_curquarter.setVisibility(View.GONE);
                        iv_curyear.setVisibility(View.GONE);
                        break;
                }

                if (!start_time.isEmpty()) {
                    tv_starttime.setText(start_time);

                } else {
                    tv_starttime.setText("请选择开始时间");
                }

                if (!end_time.isEmpty()) {
                    tv_endtime.setText(end_time);
                } else {
                    tv_endtime.setText("请选择结束时间");
                }

                contentView.findViewById(R.id.layout_gray).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        replay.hideWindow();
                    }
                });

                //本月
                rl_curmonth.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iv_curmonth.setVisibility(View.VISIBLE);
                        iv_curquarter.setVisibility(View.GONE);
                        iv_curyear.setVisibility(View.GONE);

                        timetype = 0;
                    }
                });

                //本季
                rl_curquarter.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iv_curmonth.setVisibility(View.GONE);
                        iv_curquarter.setVisibility(View.VISIBLE);
                        iv_curyear.setVisibility(View.GONE);

                        timetype = 1;
                    }
                });

                //本年
                rl_curyear.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iv_curmonth.setVisibility(View.GONE);
                        iv_curquarter.setVisibility(View.GONE);
                        iv_curyear.setVisibility(View.VISIBLE);

                        timetype = 2;
                    }
                });

                //开始时间
                tv_starttime.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iv_curmonth.setVisibility(View.GONE);
                        iv_curquarter.setVisibility(View.GONE);
                        iv_curyear.setVisibility(View.GONE);

                        starttime.showTimePickerView();
                        replay.hideWindow();
                        timetype = 3;
                    }
                });

                //结束时间
                tv_endtime.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iv_curmonth.setVisibility(View.GONE);
                        iv_curquarter.setVisibility(View.GONE);
                        iv_curyear.setVisibility(View.GONE);

                        endtime.showTimePickerView();
                        replay.hideWindow();
                        timetype = 3;
                    }
                });

                //确定
                btn_sure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (timetype) {
                            case 0:
                                text_leave_creator.setText("本月");
                                break;
                            case 1:
                                text_leave_creator.setText("本季");
                                break;
                            case 2:
                                text_leave_creator.setText("本年");
                                break;
                            case 3:
                                text_leave_creator.setText("自定义时间");
                                break;
                        }
                        getLeaveTypeStatistics();
                        replay.hideWindow();
                    }
                });

            }
        };
        replay.showWindowsTop(fg_leave_top);
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_without_address_personnel, R.id.aty_without_address_date})
    public void onClick(View view) {
        switch (view.getId()) {
            //指定人员
            case R.id.aty_without_address_personnel:
                startActivityForResult(new Intent(this, PersonnelSelectActivity.class), 0);
                break;
            //指定时间
            case R.id.aty_without_address_date:
                showPop(start_time, end_time, timetype);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        } else {
            switch (requestCode) {
                case 0:
                    String type = data.getStringExtra("usertype");
                    text_leave_state.setText(type);
                    if (type.equals("我自己")) {
                        usertype = "0";
                    } else if (type.equals("自选")) {
                        usertype = "1";
                    } else if (type.equals("全部")) {
                        usertype = "2";
                        if (data.getStringExtra("users") != null) {
                            users = data.getStringExtra("users");
                        } else {
                            users = null;
                        }
                        if (data.getStringExtra("depars") != null) {
                            depars = data.getStringExtra("depars");
                        } else {
                            depars = null;
                        }
                    }
                    getLeaveTypeStatistics();
                    break;
            }
        }
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {

    }
}
